import storage from 'store'
import { login, GetUserInfo, logout, getAccessToken } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        // storage.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
        // commit('SET_TOKEN', token)
        // resolve()
        login(userInfo).then(response => {
          const result = response.result
          storage.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', result.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    getAccessToken({ commit }, values) {
      return new Promise((resolve, reject) => {
        getAccessToken(values).then(response => {
          if (response.code == 200) {
            storage.set(ACCESS_TOKEN, response.data.accessToken, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', response.data.accessToken)
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        // 注释异步
        GetUserInfo().then(Response => {
          // storage.set(ACCESS_TOKEN, Response.data.token, 7 * 24 * 60 * 60 * 1000)
          // commit('SET_TOKEN', Response.data.token)
          if(Response.code === 200){
            resolve(Response)
            commit('SET_INFO', Response.data.name)
          }
        })
          .catch(error => {
            reject(error)
          })
        // 本地
        // resolve()
      })

      // getInfo().then(response => {
      // const result = response.result
      // console.log("getInfo",response)

      // resolve(response)
      //   if (result.role && result.role.permissions.length > 0) {
      //     const role = result.role
      //     role.permissions = result.role.permissions
      //     role.permissions.map(per => {
      //       if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
      //         const action = per.actionEntitySet.map(action => { return action.action })
      //         per.actionList = action
      //       }
      //     })
      //     role.permissionList = role.permissions.map(permission => { return permission.permissionId })
      //     commit('SET_ROLES', result.role)
      //     commit('SET_INFO', result)
      //   } else {
      //     reject(new Error('getInfo: roles must be a non-null array !'))
      //   }

      //   commit('SET_NAME', { name: result.name, welcome: welcome() })
      //   commit('SET_AVATAR', result.avatar)

      // resolve(response)
      // }).catch(error => {
      //   reject(error)
      // })
      // })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let prefix = GolbUrl.uaaUri;
        let accessToken = storage.get(ACCESS_TOKEN)
        localStorage.removeItem("Access-Token");
        localStorage.removeItem("LH_WEB_CODE");
        location.reload();
        let logoutUrl = prefix + "/oauth/remove/token?access_token=" + accessToken + '&redirect_uri=' + GolbUrl.redirect_uri;
        window.location.href = logoutUrl;
        // commit('SET_TOKEN', '')
        // commit('SET_ROLES', [])
        // storage.remove(ACCESS_TOKEN)
        // resolve()
        // // logout().then((res) => { }).catch((err) => { })
        // logout(state.token).then(() => {
        //   resolve()
        //   localStorage.removeItem("token")
        // }).catch(() => {
        //   resolve()
        // }).finally(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   storage.remove(ACCESS_TOKEN)
        // })
      })
    }

  }
}

export default user
